package com.example.springboot.service.admin.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.mapper.MetasMapper;
import com.example.springboot.model.entity.Metas;
import com.example.springboot.model.form.PageUtil;
import com.example.springboot.model.form.Result;
import com.example.springboot.model.req.admin.MetasReq;
import com.example.springboot.model.vo.MetasVO;
import com.example.springboot.service.admin.MetasService;
import com.example.springboot.utils.CopyUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zf
 * @分类表
 * @since 2021-03-17
 */
@Service
public class MetasServiceImpl extends ServiceImpl<MetasMapper, Metas> implements MetasService {

    @Override
    public Result getPage(MetasReq req) {

        List<Metas> metas = list(new LambdaQueryWrapper<Metas>()
                .eq(Metas::getInvalid, 0)
                .orderByAsc(Metas::getSort)
                .orderByDesc(Metas::getCreateTime));

        return Result.buildSuccess(metas.stream().map(e -> {
            MetasVO VO = CopyUtil.copyObject(e, MetasVO.class);
            return VO;
        }));
    }

    @Override
    public Result addInfo(MetasReq req) {
        //主表复制元素
        Metas metas = CopyUtil.copyObject(req, Metas.class);
        boolean flag;
        if (req.getId() != null) {
            flag = updateById(metas);
        } else {
            flag = save(metas);
        }
        if (flag) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

    @Override
    public Result deleteInfos(String ids) {
        if (update(new UpdateWrapper<Metas>().lambda()
                .set(Metas::getInvalid, 1)
                .inSql(Metas::getId, ids)
        )) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

}
